clear
est clear

	use "$input/full_geoid_pair_panel.dta"
	
	bysort state_fips: egen max=max(storm)
		drop if max!=1

	replace migration=migration/(tot_pop/100000)
	
			
	preserve
		collapse (sum) migration ,by(to_geoid move_year )

			rename migration in_migration
			rename to_geoid from_geoid
			save "$input/temp1.dta", replace
	restore
	
	
		collapse (sum) migration (mean) gdp,by(from_geoid move_year  state_fips)
	
		merge 1:1 from_geoid move_year using "$input/temp1.dta"
		
		g net_migration=in_migration-migration
		
		g count=1 if net_migration!=.
		
		bysort from_geoid: egen total=total(count)
			drop if total!=27
			drop count total
		
		collapse (sum) net_migration (mean) gdp, by(from_geoid)
		
		egen mean_gdp=mean(gdp)
		egen sd_gdp=sd(gdp)
		egen mean_mig=mean(net)
		egen sd_mig=sd(net)
		g z_gdp=(gdp-mean_gdp)/sd_gdp
		g z_mig=(net-mean_mig)/sd_mig
		
		twoway (scatter z_mig z_gdp ,m(C) mc(navy%10)) ///
						(lfit z_mig z_gdp , lc(gs4) lp(dash) ///
						yti("Z-score of total net migration", size(large)) xtitle("Z-score of GDP", size(large)) xlab(, nogrid labsize(vlarge)) ylabel(,nogrid labsize(vlarge)) ysize(12) xsize(12) legend(off))	
						
		graph export "$figures/fig_si3.png", as(png) replace
		
		
		drop if (z_gdp>10)
			
		twoway (scatter z_mig z_gdp ,m(C) mc(navy%10)) ///
						(lfit z_mig z_gdp , lc(gs4) lp(dash) ///
						yti("Z-score of total net migration", size(large)) xtitle("Z-score of GDP", size(large)) xlab(, nogrid labsize(vlarge)) ylabel(,nogrid labsize(vlarge)) ysize(12) xsize(12) legend(off))	
						
		graph export "$figures/fig_4a.png", as(png) replace

			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			